var fieldId = -1;
//Class Field :
function Field(name, reachables, deepness){
	this.name = name; 				// Name of the field (currently unused)
	this.id = (fieldId += 1);		// ID for numbering the fields
	this.reachables = reachables; 	// Fields reachable from this field
	this.deepness = deepness;		// Deepness of the field (for the change in the tables)
	
	/* Depths:
	 * [-1] Entrance;
	 * [ 0] Gnomes; Draenei;
	 * [ 1] Orcs; Goblins; Dwarves; Elves;
	 * [ 2] Trolls; Humans;
	 * [ 3] Minotaur and deeper fields (treasures)
	 */	
	
	// Display the name instead of [object] (currently unused)
	this.toString = function(){
		return this.name;
	};
	
	// Calculate the path according to the rules
	this.getRealPath = function(targetField){
		var pathRand = getRandomInt(1, 10);  //putting the 0 at the and simplify the code		
		if(pathRand > 4 && pathRand < 7 && this.id < 10){ //wrong way (disabled for treasures)
			random = getRandomInt(1, this.reachables.length-1);
			if( random === targetField ){ //switching forbidden value with the last 0 one
				random = 0;
			}
			return this.reachables[random];
		}
		else if(pathRand > 9-this.deepness && this.id < 9){ // go to the minotaur field if not already here
			alert("You got lost in the maze and went directly to the minotaur!");
			return 9;
		}
		else if(pathRand > 6 && this.id > 0 && this.id < 9){ // go to the entrance if not already here
			alert("You got lost in the maze and went directly to the entrance!");
			EntranceResetTreasuresTrigger();
			return 0;
		}
		else{ // right way
			return targetField;
		}		
	};
}

// Global map fields (zero-based index / ID)
var MapFields = [	
	new Field(	'Entrance',				[1,2],			-1),
	new Field(	'Gnomes territory',		[0,2,3,4],		0),
	new Field(	'Draenei territory',	[0,1,5,6],		0),
	new Field(	'Orcs territory',		[1,4,7],		1),
	new Field(	'Goblins territory',	[1,3,5,7],		1),
	new Field(	'Dwarves territory',	[2,4,6,8],		1),
	new Field(	'Elves territory',		[2,5,8],		1),
	new Field(	'Trolls territory',		[3,4,9],		2),
	new Field(	'Humans territory',		[5,6,9],		2),
	new Field(	'Minotaure territory',	[7,8,10,11],	3),
	new Field(	'Treasure 1',			[9],			3),
	new Field(	'Treasure 2',			[9],			3)
];		

// Highlight current map position
function ShowCurrentPosition()
{
	for(var j = 0; j < 12; j++)
	{
		$("#mapItem" + j).css("border", "");
		$("#mapItem" + j).removeClass("currentPosition");
	}
	$('#mapItem' + WebPlayer.position).addClass("currentPosition");
}

// Highlights reachable positions on the map
function ShowReachablePositions()
{
	for(var j = 0; j < 12; j++)
	{
		$("#mapItem" + j).removeClass("reachable");
		$("#mapItem" + rPos).css("border", "");
	}
	for(var i = 0; i < MapFields[WebPlayer.position].reachables.length; i++)
	{
		var rPos = MapFields[WebPlayer.position].reachables[i];
		$("#mapItem" + rPos).addClass("reachable");
	}
}

// Resets the treasures, when the entrance is reached
function EntranceResetTreasuresTrigger()
{
	treasureState[0] = true;
	treasureState[1] = true;	
}
